package com.umeox.watch.moto.alarm.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.umeox.moto.common.executor.ThreadExecutor;
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.watch.moto.alarm.AlarmDetail;
import com.umeox.watch.moto.alarm.R;
import com.umeox.watch.moto.alarm.base.Constants;


public class ConfirmDeleteActivity extends ThemeAppCompatActivity {

    private TextView mMessageTv;
    private ImageView mConfirmTv;
    private TextView mDoneIv;

    private static final int DELETE_COMPLETE = 0x10;
    private static final int FINISH_ACTIVITY = 0x12;

    private AlarmDetail mAlarmDetail;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_confirm_delete);
        mAlarmDetail = getIntent().getParcelableExtra(Constants.EXTRA_KEY_ALARM);
        mMessageTv = findViewById(R.id.tvMessage);
        mConfirmTv = findViewById(R.id.abConfirm);
        mDoneIv = findViewById(R.id.ivDone);

        mConfirmTv.setVisibility(View.VISIBLE);
        mDoneIv.setVisibility(View.GONE);


        mConfirmTv.setOnClickListener(v -> ThreadExecutor.getInstance().io().execute(() -> {
            mAlarmDetail.delete();
            mHandler.sendEmptyMessage(DELETE_COMPLETE);
        }));

    }

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == DELETE_COMPLETE) {
                mConfirmTv.setVisibility(View.GONE);
                mDoneIv.setVisibility(View.VISIBLE);
                mHandler.sendEmptyMessageDelayed(FINISH_ACTIVITY, 1000);
            } else if (msg.what == FINISH_ACTIVITY) {
                ConfirmDeleteActivity.this.setResult(RESULT_OK, new Intent());
                finish();
            }
            return false;
        }
    });
}
